<?php
namespace app\massage\model;

use app\BaseModel;
use think\facade\Db;

class CashUpdateRecord extends BaseModel
{
    //定义表名
    protected $name = 'massage_coach_cash_update_record';




    /**
     * @功能说明:添加
     */
    public function dataAdd($data){

        $data['create_time'] = time();

        $res = $this->insert($data);

        return $res;

    }



    /**
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @功能说明:列表
     */
    public function dataList($dis,$page){

        $data = $this->where($dis)->order('id desc')->paginate($page)->toArray();

        return $data;

    }


    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2023-08-19 15:48
     * @功能说明:添加记录
     */
    public function recordAdd($coach_id,$cash,$is_add,$create_user=0,$text='',$info_id=0){

        $coach_model = new Coach();

        $coach_info = $coach_model->dataInfo(['id'=>$coach_id]);

        if($coach_info['service_price']<$cash&&$is_add==0){

            return ['code'=>500,'msg'=>'服务费小于扣款金额'];
        }

        $insert = [

            'uniacid' => $coach_info['uniacid'],

            'coach_id'=> $coach_id,

            'user_id' => $coach_info['user_id'],

            'cash'    => $cash,

            'is_add'  => $is_add,

            'text'    => $text,

            'before_cash'=> $coach_info['service_price'],

            'create_user'=> $create_user,

            'after_cash' => $is_add==1?$coach_info['service_price']+$cash:$coach_info['service_price']-$cash,
        ];

        $res = $this->dataAdd($insert);

        if($res==0){

            return ['code'=>500,'msg'=>'修改失败'];
        }

        $res = $coach_model->dataUpdate(['id'=>$insert['coach_id'],'service_price'=>$coach_info['service_price']],['service_price'=>$insert['after_cash']]);

        return $res;

    }





}